package com.example.tourism.controller;

import com.example.tourism.pojo.Result;
import com.example.tourism.pojo.vo.req.LoginReqVo;
import com.example.tourism.pojo.vo.req.RegisterReqVo;
import com.example.tourism.pojo.vo.req.UserInfoReqVo;
import com.example.tourism.pojo.vo.res.DoubleTokenResVo;
import com.example.tourism.pojo.vo.res.UserInfoResVo;
import com.example.tourism.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/user", produces = "application/json;charset=utf-8")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping(value = "/register")
    public Result<String> register(@RequestBody RegisterReqVo registerReqVo) {
        return userService.register(registerReqVo);
    }

    @PostMapping(value = "/sendEmail")
    public Result<String> sendVerifyCode(String email) {
        return userService.sendVerifyCode(email);
    }

    @RequestMapping("/checkVerifyCode")
    public Result<String> checkVerifyCode(String email, String verifyCode) {
        return userService.checkVerifyCode(email, verifyCode);
    }

    @RequestMapping("/login")
    public Result<DoubleTokenResVo> login(@RequestBody LoginReqVo loginReqVo, HttpServletResponse response) {

        return userService.login(loginReqVo, response);
    }

    @GetMapping("/refreshToken")
    public Result<String> refreshToken(String refreshToken, HttpServletResponse response) {

        return userService.refreshToken(refreshToken, response);
    }

    @GetMapping("/info")
    public Result<UserInfoResVo> info(HttpServletRequest request) {
        return userService.getUserInfo(request);
    }

    @PostMapping(value = "/updateUserInfo")
    public Result updateUserInfo(@RequestBody UserInfoReqVo user, HttpServletRequest request) {
        return userService.updateUserInfo(user, request);
    }

    @PostMapping("/updateUserAvatar")
    public Result updateUserAvatar(@RequestParam("id") String id,@RequestParam("avatarUrl") String avatarUrl) {
        System.out.println("更新头像进入"+id+avatarUrl);
        return userService.updateUserAvatar(id,avatarUrl);
    }

    @GetMapping("getUserAllInfo")  //获取用户所有信息
    public Result getUserAllInfo(HttpServletRequest request){
        String id= (String) request.getAttribute("Id");
        return userService.getUserAllInfo(id);
    }
}
